package com.jemframework.domain.identifier;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import com.jemframework.domain.exception.IdentifierConverterException;

public abstract class AbstractJSFIdentifierConverter implements Converter {

  public AbstractJSFIdentifierConverter() {
    super();
  }

  protected abstract Identifier createIdentifier(String aString) throws IdentifierConverterException;

  protected abstract String getIdentifierAsString(Identifier anIdentifier);

  public Object getAsObject(FacesContext context, UIComponent component, String identifierAsString) {

    try {

      Identifier entityId = createIdentifier(identifierAsString);
      return entityId;

    } catch (IdentifierConverterException e) {
      throw new ConverterException(e);
    }
  }

  public String getAsString(FacesContext context, UIComponent component, Object object) {
    Identifier entityId = (Identifier) object;
    String identifierAsString = getIdentifierAsString(entityId);
    return identifierAsString;
  }
}
